package com.pig4cloud.pig.bshf.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import com.pig4cloud.plugin.excel.annotation.RequestExcel;
import com.pig4cloud.pig.bshf.entity.BshfEntity;
import com.pig4cloud.pig.bshf.service.BshfService;

import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import com.pig4cloud.pig.common.security.annotation.HasPermission;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 辨识回访表
 *
 * @author pig
 * @date 2025-05-12 15:54:10
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/bshf" )
@Tag(description = "bshf" , name = "辨识回访表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BshfController {

    private final  BshfService bshfService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param bshf 辨识回访表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @HasPermission("bshf_bshf_view")
    public R getBshfPage(@ParameterObject Page page, @ParameterObject BshfEntity bshf) {
        LambdaQueryWrapper<BshfEntity> wrapper = Wrappers.lambdaQuery();
        return R.ok(bshfService.page(page, wrapper));
    }


    /**
     * 通过条件查询辨识回访表
     * @param bshf 查询条件
     * @return R  对象列表
     */
    @Operation(summary = "通过条件查询" , description = "通过条件查询对象" )
    @GetMapping("/details" )
    @HasPermission("bshf_bshf_view")
    public R getDetails(@ParameterObject BshfEntity bshf) {
        return R.ok(bshfService.list(Wrappers.query(bshf)));
    }

    /**
     * 新增辨识回访表
     * @param bshf 辨识回访表
     * @return R
     */
    @Operation(summary = "新增辨识回访表" , description = "新增辨识回访表" )
    @SysLog("新增辨识回访表" )
    @PostMapping
    @HasPermission("bshf_bshf_add")
    public R save(@RequestBody BshfEntity bshf) {
        return R.ok(bshfService.save(bshf));
    }

    /**
     * 修改辨识回访表
     * @param bshf 辨识回访表
     * @return R
     */
    @Operation(summary = "修改辨识回访表" , description = "修改辨识回访表" )
    @SysLog("修改辨识回访表" )
    @PutMapping
    @HasPermission("bshf_bshf_edit")
    public R updateById(@RequestBody BshfEntity bshf) {
        return R.ok(bshfService.updateById(bshf));
    }

    /**
     * 通过id删除辨识回访表
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除辨识回访表" , description = "通过id删除辨识回访表" )
    @SysLog("通过id删除辨识回访表" )
    @DeleteMapping
    @HasPermission("bshf_bshf_del")
    public R removeById(@RequestBody Integer[] ids) {
        return R.ok(bshfService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param bshf 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @HasPermission("bshf_bshf_export")
    public List<BshfEntity> exportExcel(BshfEntity bshf,Integer[] ids) {
        return bshfService.list(Wrappers.lambdaQuery(bshf).in(ArrayUtil.isNotEmpty(ids), BshfEntity::getId, ids));
    }

    /**
     * 导入excel 表
     * @param bshfList 对象实体列表
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @HasPermission("bshf_bshf_export")
    public R importExcel(@RequestExcel List<BshfEntity> bshfList, BindingResult bindingResult) {
        return R.ok(bshfService.saveBatch(bshfList));
    }
}
